<template>
  <a-layout>
    <a-layout-content :style="{ backgroundColor: 'white', padding: '24px', margin: 0, minHeight: '280px' }">
      <h3 v-if="level1.length === 0">对不起，找不到相关文档！</h3>
      <a-row>
        <a-col :span="4">
          <a-tree
            v-if="level1.length > 0"
            :selectedKeys="defaultselectedKeys"
            :tree-data="level1"
            @select="onSelect"
            :field-names="{ key: 'id', title: 'name', value: 'id' }"
            :defaultExpandAll="true"
          >
          </a-tree>
        </a-col>
        <!-- 文档内容 富文本-->
        <a-col :span="20">
          <div>
            <h2>{{doc.name}}</h2>
            <div>
              <span>阅读数：{{doc.viewCount}}</span> &nbsp;&nbsp;
              <span>点赞数：{{doc.voteCount}}</span>
            </div>
            <a-divider style="height: 2px; background-color: #9999cc"/>
          </div>
          <div class="editor-content-view" :innerHTML="html"></div>
          <!-- 点赞 -->
          <div class="vote-div">
            <a-button type="primary" shape="round" size="large" @click="vote">
              <LikeOutlined /> &nbsp;点赞：{{doc.voteCount}}
            </a-button>
          </div>
        </a-col>
      </a-row>
    </a-layout-content>
  </a-layout>
</template>

<script lang="ts">
import {onMounted, ref,} from 'vue';
import axios from "axios";
import {message} from "ant-design-vue";
import {Tool} from "@/util/tool";
import {useRoute} from 'vue-router';
import '@wangeditor/editor/dist/css/style.css'
import { LikeOutlined } from '@ant-design/icons-vue';

export default {
  name: "DocView",
  components: {
    LikeOutlined,
  },
  setup() {
    const route = useRoute();
    // 树形结构
    const level1 = ref();
    level1.value = [];

    // 默认选中的节点
    const defaultselectedKeys = ref();
    // 当前选中的文档
    const doc = ref();
    doc.value = {};

    // 树组件选择时触发
    const onSelect = (selectedKeys: any, info: any) => {
      console.log("selected", selectedKeys);
      if (Tool.isNotEmpty(selectedKeys)) {
        // 选中某一个节点时，加载该节点文档信息
        doc.value = info.selectedNodes[0];
        console.log(info.selectedNodes[0])
        handleQueryContent(selectedKeys[0]);
      }
    }

    /**
     * 数据查询
     **/
    const handleQuery = () => {
      // 这里get传参不用拼接方法 使用params固定参数配置
      axios.get("/doc/all/" + route.query.ebookId).then((response) => {
        const data = response.data;
        if (data.success) {
          //level1.value = data.content; // 这是数组结构
          level1.value = Tool.array2Tree(data.content, 0); // 自己处理成树形结构
          console.log("树形结构：", level1.value);
          // 初始自动选择第一个文章
          if (Tool.isNotEmpty(level1.value)) {
            //defaultselectedKeys.value = [level1.value[0].id];
            handleQueryContent(level1.value[0].id);
            // 初始显示文档信息
            doc.value = level1.value[0];
          }
        } else {
          message.error(data.message);
        }
      });
    }

    /**
     * 内容查询 - 富文本
     **/
    const html = ref(); // html 代码内容
    const handleQueryContent = (id: number) => {
      // 这里get传参不用拼接方法 使用params固定参数配置
      axios.get("/doc/find-content/" + id).then((response) => {
        const data = response.data;
        if (data.success) {
          html.value = data.content;
        } else {
          message.error(data.message);
        }
      });
    }

    // 点赞
    const vote = () => {
      axios.get('/doc/vote/' + doc.value.id).then((response) => {
        const data = response.data;
        if (data.success) {
          doc.value.voteCount++;
        } else {
          message.error(data.message);
        }
      })
    }

    onMounted(() => {
      handleQuery();
    });

    return {
      level1,
      html,
      onSelect,
      defaultselectedKeys,
      doc,
      vote
    };
  },
}
</script>

<style>
/**
  wangeditor 默认样式
 */
.editor-content-view p,
.editor-content-view li {
  white-space: pre-wrap; /* 保留空格 */
}

.editor-content-view blockquote {
  border-left: 8px solid #d0e5f2;
  padding: 10px 10px;
  margin: 10px 0;
  background-color: #f1f1f1;
}

.editor-content-view code {
  font-family: monospace;
  background-color: #eee;
  padding: 3px;
  border-radius: 3px;
}
.editor-content-view pre>code {
  display: block;
  padding: 10px;
}

.editor-content-view table {
  border-collapse: collapse;
}
.editor-content-view td,
.editor-content-view th {
  border: 1px solid #ccc;
  min-width: 50px;
  height: 20px;
}
.editor-content-view th {
  background-color: #f1f1f1;
}

.editor-content-view ul,
.editor-content-view ol {
  padding-left: 20px;
}

.editor-content-view input[type="checkbox"] {
  margin-right: 5px;
}
.vote-div {
  padding: 15px;
  text-align: center;
}
</style>